﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_EditDistances : System.Web.UI.Page
{
	private Int64 RouteID { get { return DS.ToInt64(Request.QueryString["id"]); } }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			InitControls();
		}
	}

	protected void InitControls()
	{
		RouteStationM2M rsM2M = new RouteStationM2M();
		rsM2M.Route_id = RouteID;
		IList<RouteStationM2M> RouteStationCollection = DBEntity<RouteStationM2M>.Select();
		rpt.DataSource = RouteStationCollection;
		rpt.DataBind();
	}

	protected void btnSaveDistances_Click(object sender, EventArgs e)
	{
		foreach (RepeaterItem elem in rpt.Items)
		{
			double distance = 0;
			try { distance = Convert.ToDouble((elem.FindControl("tbDistance") as TextBox).Text); }
			catch { distance = 0; }
			Int64 id = DS.ToInt64((elem.FindControl("hfID") as HiddenField).Value);
			if (id > 0)
			{
				RouteStationM2M rsm2m = DBEntity<RouteStationM2M>.Load(id);
				rsm2m.Distance = distance;
				rsm2m.Merge();
			}
		}

		Response.Redirect(ResolveUrl("~/Pages/RouteEdit.aspx?Id=" + RouteID.ToString()));
	}

	protected void btnCancel_Click(object sender, EventArgs e)
	{
		Response.Redirect(ResolveUrl("~/Pages/RouteEdit.aspx?Id=" + RouteID.ToString()));
	}
}